package org.facyt.vacas.entities.login;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
@Table(name="usuario", schema="aplicaciones")
public class Usuario implements Serializable, UserDetails {

	@Id
	@SequenceGenerator(name="usuario_seq", sequenceName="aplicaciones.usuario_id_seq")
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="usuario_seq")
	@Column(name="id")
	private Integer id;
	
	@Column(name="user_name")
	private String userName;
	
	@Column(name="password")
	private String password;
	
	@ManyToOne()
	@JoinColumn(name="fk_persona")
	private Persona persona;
	
	@ManyToOne
	@JoinColumn(name="fk_grupo")
	private Grupo grupo;
	
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(schema="aplicaciones", name="usuario_perfiles" , 
			joinColumns=@JoinColumn(name="fk_usuario"),
			inverseJoinColumns=@JoinColumn(name="fk_perfil"))
	private List<Perfil> perfiles = new ArrayList<Perfil>(); 

	public List<Perfil> getPerfiles() {
		return perfiles;
	}

	public void setPerfiles(ArrayList<Perfil> perfiles) {
		this.perfiles = perfiles;
	}

	public Integer getId() {
		return id;
	}
	
	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Persona getPersona() {
		return persona;
	}

	public void setPersona(Persona persona) {
		this.persona = persona;
	}

	public Grupo getGrupo() {
		return grupo;
	}

	public void setGrupo(Grupo grupo) {
		this.grupo = grupo;
	}

	public Collection<? extends GrantedAuthority> getAuthorities() {
		return this.getPerfiles();
	}

	public String getUsername() {
		
		return userName;
	}

	public boolean isAccountNonExpired() {
		return true;
	}

	public boolean isAccountNonLocked() {
		return true;
	}

	public boolean isCredentialsNonExpired() {
		return true;
	}

	public boolean isEnabled() {
		return true;
	}
	
	
	
	
}
